package com.youdianstar.app.common.ex

import android.content.Context
import android.graphics.Typeface
import android.text.TextUtils
import android.view.View
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.youdianstar.app.common.util.AntiShakeUtil
import com.youdianstar.app.common.util.SpanUtil


/**
 * File: KtxViewExt
 * Author: 82149 Create: 2021/9/23 15:00
 * Changes (from 2021/9/23)
 * --------------------------------------------------
 * description:  重写view click
 * ---------------------------------------------------
 *
 */

fun View.setThrottleListener(action:()->Unit){
    this.setOnClickListener {
        if (AntiShakeUtil.isInvalidClick(it))
            return@setOnClickListener
        action.invoke()
    }
}

//获取 控件颜色
fun getViewColor(context : Context,colorId : Int) :Int{
    return ContextCompat.getColor(context,colorId)
}

fun String?.parseTx():String{
    return if (TextUtils.isEmpty(this)) "--" else this!!
}

/**
 * 设置协议下划线
 *
 * @param txView 文本View
 * @param content 下划线文本
 */
fun setAgreementLine(txView: TextView,part1Tx:String,content:String,part2Tx:String){
    SpanUtil.create()
        .addSection(part1Tx+content+part2Tx)
        .setUnderline(content)
        .showIn(txView)
}



//修改字体样式
fun EditText.setFontTypeface(fontPath:String){
    val assetManager = this.context.assets
    val tf : Typeface = Typeface.createFromAsset(assetManager, fontPath)
    typeface = tf
}

fun TextView.setFontTypeface(fontPath:String){
    val assetManager = this.context.assets
    val tf : Typeface = Typeface.createFromAsset(assetManager, fontPath)
    typeface = tf
}

fun Button.setFontTypeface(fontPath:String){
    val assetManager = this.context.assets
    val tf : Typeface = Typeface.createFromAsset(assetManager, fontPath)
    typeface = tf
}